﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.ADF;
using ESRI.ArcGIS.Display;
using PlanningGIS.Util.Management;
using PlanningGIS.Engine.Controls;

namespace PlanningGIS.Symbol
{
    /// <summary>
    /// 作用：RGB组合渲染
    /// 作者：
    /// 修改日期：2009-06-19
    /// </summary>
    public partial class RasterRGB : UserControl
    {
        /// <summary>
        /// 
        /// </summary>
        private IRasterLayer m_pRasterLayer;
        /// <summary>
        /// 
        /// </summary>
        public IRasterLayer RasterLayer
        {
            get { return m_pRasterLayer; }
            set
            {
                m_pRasterLayer = value;
                InitialRgbFrame();
            }
        }
        /// <summary>
        /// 
        /// </summary>
        public RasterRGB()
        {
            InitializeComponent();
            backgroundColor.ImageVisiable = false;
            backgroundColor.InitStyle();
            colorNoData.ImageVisiable = false;
            colorNoData.InitStyle();
        }

        /// <summary>
        /// 初始化RGB符号化页面
        /// </summary>
        private void InitialRgbFrame()
        {

            IRasterRenderer pRasterRender;
            IRasterRGBRenderer pRasterRGBRender = null;
            if (m_pRasterLayer != null)
            {
                if (m_pRasterLayer.Renderer is IRasterRGBRenderer)
                {
                    pRasterRGBRender = m_pRasterLayer.Renderer as IRasterRGBRenderer;
                }
                else
                {
                    pRasterRGBRender = new RasterRGBRendererClass();
                    pRasterRender = pRasterRGBRender as IRasterRenderer;
                    pRasterRender.Raster = m_pRasterLayer.Raster;
                }
                AddRowOfRGBGrid(pRasterRGBRender);
            }
            else
                return;
            IRasterStretch3 pRasterStretch3 = pRasterRGBRender as IRasterStretch3;
            this.cbxUseGammaStretch.Checked = pRasterStretch3.UseGamma;
            if (!pRasterStretch3.UseGamma)
                this.txtGammaValue1.Enabled = false;
            else
            {
                double[] dValue = (double[])pRasterStretch3.GammaValue;
                this.txtGammaValue1.Text = dValue[0].ToString();
                this.txtGammaValue2.Text = dValue[1].ToString();
                this.txtGammaValue3.Text = dValue[2].ToString();
            }

            //拉伸
            IRasterStretch2 pRasterStretch = pRasterRGBRender as IRasterStretch2;
            if (pRasterStretch != null)
            {
                //拉伸类型
                switch (pRasterStretch.StretchType)
                {
                    case esriRasterStretchTypesEnum.esriRasterStretch_NONE:
                        this.cmbStrechStyle.SelectedIndex = 0;
                        break;
                    case esriRasterStretchTypesEnum.esriRasterStretch_Custom:
                        this.cmbStrechStyle.SelectedIndex = 1;
                        break;
                    case esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations:
                        this.cmbStrechStyle.SelectedIndex = 2;
                        break;
                    case esriRasterStretchTypesEnum.esriRasterStretch_HistogramEqualize:
                        this.cmbStrechStyle.SelectedIndex = 3;
                        break;
                    case esriRasterStretchTypesEnum.esriRasterStretch_MinimumMaximum:
                        this.cmbStrechStyle.SelectedIndex = 4;
                        break;
                    case esriRasterStretchTypesEnum.esriRasterStretch_HistogramSpecification:
                        this.cmbStrechStyle.SelectedIndex = 5;
                        break;
                    default:
                        this.cmbStrechStyle.SelectedIndex = 0;
                        break;
                }

                //颜色反向设置
                chkInvert.Checked = pRasterStretch.Invert;
                if (pRasterStretch.StretchType == esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations)
                {
                    txtDeviation.Text = pRasterStretch.StandardDeviationsParam.ToString();
                    txtDeviation.Enabled = true;
                }
                else
                {
                    txtDeviation.Text = "";
                    txtDeviation.Enabled = false;
                }
                //背景色
                chkShowBackColorRGB.Checked = pRasterStretch.Background;
                txtR.Enabled = chkShowBackColorRGB.Checked;
                txtG.Enabled = chkShowBackColorRGB.Checked;
                txtB.Enabled = chkShowBackColorRGB.Checked;
                backgroundColor.SelectedColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.FromRGBColor(pRasterStretch.BackgroundColor as IRgbColor);

                if (chkShowBackColorRGB.Checked)
                {
                    IRgbColor pRgbColor = pRasterStretch.BackgroundValue as IRgbColor;

                    if (pRgbColor != null)
                    {
                        txtB.Text = pRgbColor.Blue.ToString();
                        txtG.Text = pRgbColor.Green.ToString();
                        txtR.Text = pRgbColor.Red.ToString();
                    }
                }
            }
            //无值象素显示颜色
            IRasterDisplayProps pRasterDisplayProps = pRasterRGBRender as IRasterDisplayProps;
            if (pRasterDisplayProps != null)
            {
                //colorNoData.MyColor = Converter.FromRGBColor(pRasterDisplayProps.NoDataColor as IRgbColor);
                if (pRasterDisplayProps.NoDataColor.NullColor == false)
                {
                    IRgbColor pRGBNocolor = pRasterDisplayProps.NoDataColor as ESRI.ArcGIS.Display.IRgbColor;
                    this.colorNoData.SelectedColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.FromRGBColor(pRGBNocolor);
                }
            }
            this.txtGammaValue1.Enabled = this.cbxUseGammaStretch.Checked;
            this.txtGammaValue2.Enabled = this.cbxUseGammaStretch.Checked;
            this.txtGammaValue3.Enabled = this.cbxUseGammaStretch.Checked;
        }
        /// <summary>
        /// 填充RGB方式渲染的iGrid格网
        /// </summary>
        /// <param name="pRasterRGBRenderer"></param>
        private void AddRowOfRGBGrid(IRasterRGBRenderer pRasterRGBRenderer)
        {
            int iRBandIndex;
            int iGBandIndex;
            int iBBandIndex;

            pRasterRGBRenderer.QueryBandIndices(out iRBandIndex, out iGBandIndex, out iBBandIndex);

            iGridRGB.Rows.Clear();
            iGridRGB.Rows.Add(3);

            iGridRGB.Rows[0].Cells[0].Value = pRasterRGBRenderer.UseRedBand;
            iGridRGB.Rows[0].Cells[1].Value = "红波段";
            iGridRGB.Rows[0].Cells[2].Value = rgbColBand.Items[iRBandIndex].ToString();

            iGridRGB.Rows[1].Cells[0].Value = pRasterRGBRenderer.UseGreenBand;
            iGridRGB.Rows[1].Cells[1].Value = "绿波段";
            iGridRGB.Rows[1].Cells[2].Value = rgbColBand.Items[iGBandIndex].ToString();

            iGridRGB.Rows[2].Cells[0].Value = pRasterRGBRenderer.UseBlueBand;
            iGridRGB.Rows[2].Cells[1].Value = "蓝波段";
            iGridRGB.Rows[2].Cells[2].Value = rgbColBand.Items[iBBandIndex].ToString();
        }

        private void chkShowBackColorRGB_CheckedChanged(object sender, EventArgs e)
        {
            this.txtR.Enabled = this.chkShowBackColorRGB.Checked;
            this.txtG.Enabled = this.chkShowBackColorRGB.Checked;
            this.txtB.Enabled = this.chkShowBackColorRGB.Checked;
        }

        private void cbxUseGammaStretch_CheckedChanged(object sender, EventArgs e)
        {
            txtGammaValue1.Enabled = this.cbxUseGammaStretch.Checked;
            this.txtGammaValue2.Enabled = this.cbxUseGammaStretch.Checked;
            this.txtGammaValue3.Enabled = this.cbxUseGammaStretch.Checked;
        }

        /// <summary>
        /// 设置RGB渲染方式
        /// </summary>
        public void SetRGBRenderer()
        {

            IRasterRGBRenderer pRasterRGBRenderer = new RasterRGBRendererClass();
            IRasterRenderer pRasterRender = pRasterRGBRenderer as IRasterRenderer;
            pRasterRender.Raster = this.m_pRasterLayer.Raster;

            IRasterStretch2 pRasterStretch = pRasterRGBRenderer as IRasterStretch2;
            pRasterStretch.Background = chkShowBackColorRGB.Checked;

            //背景色的处理
            if (chkShowBackColorRGB.Checked)
            {
                if (txtR.Text.Trim().Length == 0) { txtR.Text = "0"; }
                if (txtG.Text.Trim().Length == 0) { txtG.Text = "0"; }
                if (txtB.Text.Trim().Length == 0) { txtB.Text = "0"; }

                object vVariant = pRasterStretch.BackgroundValue;

                System.Double[] d = (double[])vVariant;
                d[0] = Convert.ToDouble((string)txtR.Text);
                d[1] = Convert.ToDouble((string)txtB.Text);
                d[2] = Convert.ToDouble((string)txtG.Text);
                vVariant = d;

                pRasterStretch.BackgroundValue = vVariant;
                pRasterStretch.BackgroundColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(backgroundColor.SelectedColor);
            }

            //波段
            int len = iGridRGB.Rows[0].Cells[2].Value.ToString().Length;
            int iRBandIndex = Convert.ToInt32((string)iGridRGB.Rows[0].Cells[2].Value.ToString().Substring(len - 1, 1));
            len = iGridRGB.Rows[1].Cells[2].Value.ToString().Length;
            int iGBandIndex = Convert.ToInt32((string)iGridRGB.Rows[1].Cells[2].Value.ToString().Substring(len - 1, 1));
            len = iGridRGB.Rows[2].Cells[2].Value.ToString().Length;
            int iBBandIndex = Convert.ToInt32((string)iGridRGB.Rows[2].Cells[2].Value.ToString().Substring(len - 1, 1));

            iRBandIndex = iRBandIndex - 1;
            iGBandIndex = iGBandIndex - 1;
            iBBandIndex = iBBandIndex - 1;

            pRasterRGBRenderer.SetBandIndices((int)iRBandIndex, (int)iGBandIndex, (int)iBBandIndex);

            //可见与不可见的颜色波段
            string Red = iGridRGB.Rows[0].Cells[0].Value.ToString();
            if (Red == "True")
                pRasterRGBRenderer.UseRedBand = true;
            else
                pRasterRGBRenderer.UseRedBand = false;

            string Green = iGridRGB.Rows[1].Cells[0].Value.ToString();
            if (Green == "True")
                pRasterRGBRenderer.UseGreenBand = true;
            else
                pRasterRGBRenderer.UseGreenBand = false;

            string Blue = iGridRGB.Rows[2].Cells[0].Value.ToString();
            if (Blue == "True")
                pRasterRGBRenderer.UseBlueBand = true;
            else
                pRasterRGBRenderer.UseBlueBand = false;


            //无值象素显示颜色
            IRasterDisplayProps pRasterDisplayProps = pRasterRGBRenderer as IRasterDisplayProps;
            pRasterDisplayProps.NoDataColor = ESRI.ArcGIS.ADF.Connection.Local.Converter.ToRGBColor(colorNoData.SelectedColor);
            if (colorNoData.SelectedColor.Name == "ffedeada" || colorNoData.SelectedColor.Name == "Control")
            {
                pRasterDisplayProps.NoDataColor.NullColor = true;
                pRasterDisplayProps.TransparencyValue = 100;
            }

            //拉伸
            pRasterStretch = pRasterRGBRenderer as IRasterStretch2;
            switch (this.cmbStrechStyle.Text)
            {
                case "不拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_NONE;
                    break;
                case "标准差方式拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations;
                    break;
                case "最大最小值方式拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_MinimumMaximum;
                    break;
                case "自定义式拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_Custom;
                    break;
                case "直方图均衡法拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_HistogramEqualize;
                    break;
                case "柱状图方式拉伸":
                    pRasterStretch.StretchType = esriRasterStretchTypesEnum.esriRasterStretch_HistogramSpecification;
                    break;
            }

            IRasterStretch3 pRasterStretch3 = pRasterRGBRenderer as IRasterStretch3;

            if (this.cbxUseGammaStretch.Checked)
                    {
                        pRasterStretch3.UseGamma = this.cbxUseGammaStretch.Checked;

                        try
                        {
                            double[] dValue = (double[])pRasterStretch3.GammaValue;
                            dValue[0] = double.Parse(this.txtGammaValue1.Text.ToString());
                            dValue[1] = double.Parse(this.txtGammaValue2.Text.ToString());
                            dValue[2] = double.Parse(this.txtGammaValue3.Text.ToString());
                            pRasterStretch3.GammaValue = dValue;
                        }
                        catch
                        {
                        }
                    }
                    else
                    {
                        pRasterStretch3.UseGamma = false;
                    }
            //switch (this.cmbStrechStyle.Text)
            //{
            //    case "不拉伸":
            //    case "标准差方式拉伸":
            //    case "最大最小值方式拉伸":
                    
            //        break;
            //    default:
            //        pRasterStretch3.UseGamma = false;
            //        break;
            //}

            pRasterStretch.Invert = chkInvert.Checked;

            if (pRasterStretch.StretchType == esriRasterStretchTypesEnum.esriRasterStretch_StandardDeviations)
            {
                if (txtDeviation.Text.Trim().Length == 0)
                    txtDeviation.Text = "1";
                if (txtDeviation.Text == "0")
                    txtDeviation.Text = "1";
                pRasterStretch.StandardDeviationsParam = Convert.ToDouble((string)txtDeviation.Text);
            }

            pRasterRender.Update();
            this.m_pRasterLayer.Renderer = pRasterRGBRenderer as IRasterRenderer;
        }
    }
}
